package edu.jkd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.jkd.entity.*;
import edu.jkd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("film")
public class FilmController {
    @Autowired
    private IFilmService filmService;
    @GetMapping()
    public ModelAndView getAllFilm(){
        ModelAndView mav=new ModelAndView("film_list");
        List<Film> filmList =filmService.list();
        mav.addObject("filmList",filmList);
        return mav;
    }

    @Autowired
    private IDirectorService directorService;
    @Autowired
    private IActorService actorService;
    @Autowired
    private IStillsService stillsService;
    @Autowired
    private ITypeService typeService;
    @Autowired
    private ITfService tfService;
    @GetMapping("/{id}")
    public ModelAndView getByflimId(@PathVariable("id") Integer id){

        LambdaQueryWrapper<Film> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Film::getFilmProid,id);
        Film film = filmService.getOne(queryWrapper1);
        String ctime=film.getFilmCtime().toString().replace("T"," ");
        String duration=film.getFilmDuration().toString().replace(".0"," ");

        LambdaQueryWrapper<Director> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(Director::getDrFilmid,film.getFilmId());
        Director director = directorService.getOne(queryWrapper2);

        LambdaQueryWrapper<Actor> queryWrapper3=new LambdaQueryWrapper<>();
        queryWrapper3.eq(Actor::getActorFilmid,film.getFilmId());
        List<Actor> actorList = actorService.list(queryWrapper3);

        LambdaQueryWrapper<Stills> queryWrapper4=new LambdaQueryWrapper<>();
        queryWrapper4.eq(Stills::getStillsFilmid,film.getFilmId());
        List<Stills> stillsList = stillsService.list(queryWrapper4);

        LambdaQueryWrapper<Tf> queryWrapper5=new LambdaQueryWrapper<>();
        queryWrapper5.eq(Tf::getfId,film.getFilmId());
        List<Tf> tfList = tfService.list(queryWrapper5);

        List<Type> typeList =new ArrayList<Type>();
        for (int i=0;i<tfList.size();i++) {
            Type type=typeService.getById(tfList.get(i).gettId());
            typeList.add(type);
        }


        ModelAndView mav=new ModelAndView("xqym");
        mav.addObject("ctime",ctime);
        mav.addObject("duration",duration);
        mav.addObject("film",film);
        mav.addObject("director",director);
        mav.addObject("actorList",actorList);
        mav.addObject("stillsList",stillsList);
        mav.addObject("typeList",typeList);
        return mav;
    }
}
